package com.bw.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.bw.common.domain.*;
import com.bw.common.result.Result;
import com.bw.shop.config.FastUtil;
import com.bw.shop.mapper.ShopMapper;
import com.bw.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author：HaoYunde
 * @Package：com.bw.shop.service.impl
 * @Project：hyd-boot
 * @name：ShopServiceImpl
 * @Date：2024/6/17 10:44
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    public ShopMapper shopMapper;
    @Autowired
    public FastUtil fastUtil;
    @Autowired
    public StringRedisTemplate redisTemplate;


    @Value("${fdfs.web-server-url}")
    private String addr;

    @Override
    public Result addList(Shop shop) {
        Integer i= shopMapper.addList(shop);
        if(i>0){
            List<String> imgs = shop.getImgs();
            for (String img : imgs) {
                Pic pic = new Pic();
                pic.setImg(img);
                pic.setShopId(shop.getShopId());
                //添加图片表
                shopMapper.addImg(pic);
            }
        }
        return Result.success();
    }

    @Override
    public List<Shop> findList() {
        return shopMapper.findList();
    }


    @Override
    public Result upload(MultipartFile file) {
        String url="";
        try {
            url=fastUtil.upload(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //return Result.success("http://106.54.45.36:8888/"+url);
        return Result.success("http://"+addr+"/"+url);
    }

    @Override
    public List<Brand> findbrand() {
        List<Brand>list= shopMapper.findbrand();
        if(!redisTemplate.hasKey("brand")){
            for (Brand brand : list) {
                //商品品牌数据需要在项目启动的时候同步到Redis
                redisTemplate.opsForList().rightPush("brand", JSON.toJSONString(brand));
            }
        }
        return list;
    }

    @Override
    public List<Lx> findLx() {
        List<Lx> list= shopMapper.findLx();
        return list;
    }

    @Override
    public Result addCarList(Car car) {
        Integer i = shopMapper.addCarList(car);
        return Result.success();
    }


}
